<?php
ini_set('max_execution_time', 6000);
include_once dirname(dirname(dirname(__FILE__))) . '/include/common.php';
include_once 'simple_html_dom.php';
include_once dirname(dirname(dirname(__FILE__))) . '/include/core/image.php';
$core = new core();
//get and save url navagation
$html = new simple_html_dom();
// Load HTML from a URL
$begin = time();
//$data_html = get_content_html("http://www.zara.com/fr/en/man/t-shirts/pattern/printed-t-shirt-c583032p2380762.html");
//if ($data_html['ERR'] != '') {
//    echo '<tr><td></td><td></td><td></td><td>loi crawl 01:' . $data_html['ERR'] . ' </td></tr>';
//    die;
//}
//$file = 'debug_crawl.txt';
//
//file_put_contents($file, $data_html['EXE']);
//var_dump($data_html['EXE']);die;

function get_content_html($url) {

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 300);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);

//    curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookies_get_product.txt');
//    curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookies_get_product.txt');
    curl_setopt($curl, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8');

    $result['EXE'] = curl_exec($curl);
    $result['INF'] = curl_getinfo($curl);
    $result['ERR'] = curl_error($curl);
    curl_close($curl);
    return $result;
}

function get_url_product() {
    $core = new core();
    $query = "SELECT * FROM zen_crawl_url_product WHERE is_crawled =0  ORDER BY id ASC LIMIT 1 ";
   // $query = "SELECT * FROM zen_crawl_url_product WHERE id=249 ";
    $list_nav = $core->db->get_row($query);
    if($list_nav ==null){
        return false;
    }
    $core->db->query("UPDATE zen_crawl_url_product SET note='' WHERE id=$list_nav->id");
    return $list_nav;
}

echo '<table><tr><th>url</th><th>product name</th><th>product code</th><th>error</th></tr>';
while ($product = get_url_product()) {
    if (time() - $begin > 180) {
        die("het 3 phut running!!!");
    }
    $core->db->query("UPDATE zen_crawl_url_product SET is_crawled=1 WHERE id=$product->id");
    //$render_content.='<tr colspan="4"><td>Crawl child menu ' . $level . ': '.$product->url_product.'</td></tr>';
    echo '<tr colspan="4"><td>Crawl url product  ' . $product->product_name . ': ' . $product->url_product . ' : '.$product->id.'</td></tr>';
    $time_running = time() - $begin;
    echo '<tr colspan="4"><td>time running = ' . $time_running . ', minute=' . ($time_running / 60) . '</td></tr>';

    $data_html = get_content_html($product->url_product);

    if ($data_html['ERR'] != '') {
        echo '<tr><td></td><td></td><td></td><td>loi crawl 01:' . $data_html['ERR'] . ' </td></tr>';
        die;
    }

    $html->load($data_html['EXE']);

    $repeat = 0;
    while (!$html->find('#product', 0)) {
        $repeat++;
        if ($repeat > 2) {//khong tim thay san pham->loai bo
            $core->db->query("UPDATE zen_crawl_url_product SET is_crawled=2, note='Khong tim thay san pham:".$data_html['EXE']."' WHERE id=$product->id");
            break;
        }
        //$render_content.='<tr><td></td><td></td><td></td><td>Khong tim thay menu </td></tr>';
        echo '<tr><td></td><td></td><td></td><td>Khong tim thay san pham </td></tr>';
        sleep(2);
        $data_html = get_content_html($product->url_product);
        if ($data_html['ERR'] != '') {
            echo '<tr><td></td><td></td><td></td><td>loi crawl 02:' . $data_html['ERR'] . ' </td></tr>';
            $core->db->query("UPDATE zen_crawl_url_product SET  note='".$data_html['ERR']."' WHERE id=$product->id");
            die;
        }
        $html->load($data_html['EXE']);
    }
    if ($repeat > 2) {
        continue;
    }


    $product_html = $html->find('#product', 0);
    $product_data = array();

    //get product's infomation
    //get product name
    $product_data['product_name'] = '';
    if ($product_html->find('div[class=right]', 0)) {
        $div_right = $product_html->find('div[class=right]', 0);
        if ($div_right->find('header h1', 0)) {
            $tag_h1 = $div_right->find(' header h1', 0);
            $product_data['product_name'] = trim($tag_h1->plaintext);
            if ($product_data['product_name'] == '') {
                echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT NAME</td></tr>';
                //continue;
            }
        } else {
            echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT NAME</td></tr>';
            //continue;
        }
        //get product code
        if ($div_right->find('p[class=reference]', 0)) {
            $tag_p_code = $div_right->find('p[class=reference]', 0);
            $product_data['product_code'] = trim($tag_p_code->plaintext); // Ref.5445/409
            $arr_code = explode(".", $product_data['product_code']);
            if ($arr_code) {
                $product_data['product_code'] = trim($arr_code[count($arr_code) - 1]);
            } else {
                echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT CODE</td></tr>';
                $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay PRODUCT CODE' WHERE id=$product->id");
                continue;
            }
        } else {
            echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT CODE</td></tr>';
            $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay PRODUCT CODE 02' WHERE id=$product->id");
            continue;
        }
        //get product price
        if ($div_right->find('p[class=price] span[class=price]', 0)) {
            $tag_span_price = $div_right->find('p[class=price] span[class=price]', 0);
            $html_price = $tag_span_price->outertext; //<span class="price" data-price="17.95 EUR">   </span>
            $pos_last_equal = strripos($html_price, '=');
            $pos_last_eur = strripos($html_price, 'EUR');
            $product_data['product_stamp_price'] = floatval(trim(substr($html_price, $pos_last_equal + 2, (($pos_last_eur - 2) - $pos_last_equal))));

            if ($product_data['product_stamp_price'] == '') {
                echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT PRICE</td></tr>';
                $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay PRODUCT PRICE' WHERE id=$product->id");
                continue;
            }
        } else {
            echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT PRICE</td></tr>';
            $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay PRODUCT PRICE 02' WHERE id=$product->id");
            continue;
        }

        if ($div_right->find('form', 0)) {
            $form = $div_right->find('form', 0);
            //get product color, photo

            if ($form->find('div[class=colors]', 0)) {
                $tag_div_color = $form->find('div[class=colors]', 0);
                if ($tag_div_color->find('label')) {
                    $arr_color_src = array();
                    $arr_color_name = array();

                    foreach ($tag_div_color->find('label') as $label) {
                        $color_name='';
                        if ($label->find('div span', 0)) {
                            if ($label->find('div span', 0)->plaintext != '') {
                                $color_name = $label->find('div span', 0)->plaintext;
                                $arr_color_name[] = trim($color_name);

                            }
                        }

                        if ($label->find('div img', 0) && $color_name !='') {
                            if ($label->find('div img', 0)->src != '') {
                                $arr_color_src[$color_name] = $label->find('div img', 0)->src;
                            }
                        }


                    }

                    $product_data['product_color_name'] = implode(",", $arr_color_name);
                    $product_data['product_color_src'] = $arr_color_src;
                    echo '<tr><td>product color name:' .  json_encode($arr_color_name) . '</td><td> product color src: ' . json_encode($arr_color_src) . '</td><td>////////</td><td>/////////////</td></tr>';
                    if ($product_data['product_color_name'] == "") {
                        echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>PRODUCT COLOR, SOURCE is empty: ' . $product_data['product_color_name'] . '</td></tr>';
                        $core->db->query("UPDATE zen_crawl_url_product SET  note='PRODUCT COLOR, SOURCE is empty' WHERE id=$product->id");
                        continue;
                    }

                    if (count($arr_color_name)==0) {
                        $core->db->query("UPDATE zen_crawl_url_product SET  note='san pham khong co color' WHERE id=$product->id");
                        continue;
                    }
                    //truong hop anh thumb theo color cua zara khong co, va chi co 1 color thi lay anh big photo thay the. truong hop co 2 color khong co thumb thi ko giai quyet dc
                    if (count($arr_color_src) == 0 && count($arr_color_name)==1) {
                        if ($product_html->find('div[class=left] div[class=bigImageContainer] div', 0)) {
                            $tag_div = $product_html->find('div[class=left] div[class=bigImageContainer] div', 0);
                            if ($tag_div->find('a img', 0)) {
                                $big_photo_left = $tag_div->find('a img', 0)->src;
                                if ($big_photo_left != '') {
                                    $color_name= $arr_color_name[0];
                                    $product_data['product_color_src'] = array($color_name=>$big_photo_left);

                                } else {
                                    echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay tag SOURCE IMG BIG photo product column left</td></tr>';
                                    $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay tag SOURCE IMG BIG photo product column left' WHERE id=$product->id");
                                    continue;
                                }
                            } else {
                                echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay tag IMG photo product column left</td></tr>';
                                $core->db->query("UPDATE zen_crawl_url_product SET  note='KKhong tim thay tag IMG photo product column left' WHERE id=$product->id");
                                continue;
                            }
                        } else {
                            echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay photo product column left</td></tr>';
                            $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay photo product column left' WHERE id=$product->id");
                            continue;
                        }
                    }

                } else {
                    echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT COLOR</td></tr>';
                    $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay PRODUCT COLOR' WHERE id=$product->id");
                    continue;
                }
            } else {
                echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT COLOR</td></tr>';
                $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay PRODUCT COLOR 02' WHERE id=$product->id");
                continue;
            }
            //get product size
            if ($form->find('div[id=size-btn] div[class=size-select] table', 0)) {
                $tag_table_size = $form->find('div[id=size-btn] div[class=size-select] table', 0);
                if ($tag_table_size->find('tr')) {
                    $arr_size = array();
                    foreach ($tag_table_size->find('tr') as $tr) {
                        if ($tr->find('td[class=size-name]', 0)) {
                            $arr_size[] = trim($tr->find('td[class=size-name]', 0)->plaintext);
                        }
                    }

                    $product_data['product_size'] = implode(",", $arr_size);
                    if ($product_data['product_size'] == '') {
                        echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>PRODUCT SIZE is empty</td></tr>';
                        $core->db->query("UPDATE zen_crawl_url_product SET  note='PRODUCT SIZE is empty' WHERE id=$product->id");
                        continue;
                    }
                } else {
                    echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT TR SIZE</td></tr>';
                    $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay PRODUCT TR SIZE' WHERE id=$product->id");
                    continue;
                }
            } else {
                echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay PRODUCT TABLE SIZE</td></tr>';
                $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay PRODUCT TABLE SIZE' WHERE id=$product->id");
                continue;
            }
        } else {
            echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay FORM</td></tr>';
            $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay FORM' WHERE id=$product->id");
            continue;
        }
    } else {
        echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay DIV RIGHT</td></tr>';
        $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay DIV RIGHT' WHERE id=$product->id");
        continue;
    }
    //xu lí lay danh sach big photo from javascript
    $file = 'debug_crawl.txt';

    file_put_contents($file, $data_html['EXE']);
    $pos_xmedias = strpos($data_html['EXE'],'xmedias');
    $pos_begin_data_src_photo = strpos($data_html['EXE'],'{',$pos_xmedias);
    $pos_end_data_src_photo = strpos($data_html['EXE'],'}]}}',$pos_xmedias);
    $string_data_src_photo = substr($data_html['EXE'],$pos_begin_data_src_photo,$pos_end_data_src_photo-$pos_begin_data_src_photo+4);
    $data_src_big_photo = json_decode($string_data_src_photo,true);//mảng danh sách các big photo theo từng mã màu

    $file = 'source_data_photo.txt';

    file_put_contents($file, $string_data_src_photo);

    //xu li anh main photo, extra photo
//    if(is_array($product_data['product_color_src']) && count($product_data['product_color_src']==0)){
//        //trường hợp không tìm thấy ảnh thumb theo color thì sử dụng big photo đầu tiên
//        if(is_array($data_src_big_photo)){
//            foreach ($data_src_big_photo as $code => $data_big_photo) {
//
//                $data_big_photo = $data_big_photo['xmedias'];
//                foreach ($data_big_photo as $item_photo) {
//
//                    if (isset($item_photo['url'])) {
//                        $src_real_photo = "http:" . $item_photo['url'];
//                        $product_data['product_color_src'] = array($arr_color_name[0]=>$src_real_photo);
//                        break;
//                    }
//                }
//                break;
//            }
//        }
//    }

    if (!isset($_SESSION['folder_temp'])) {
        $today = getdate();
        $_SESSION['folder_temp'] = $today["mday"] . "_" . $today["mon"] . "_" . $today["year"];
    }
    $arr_product_extra_photo = array();
    $arr_product_thumb_extra_photo = array();
    $arr_source_photo_product = array();

    $arr_source_photo_product = $product_data['product_color_src'];

    foreach ($arr_source_photo_product as $name=> $src) {
        //---> //static.zara.net/photos//2014/V/0/2/p/5445/409/800/2/w/135/5445409800_3_1_1.jpg?timestamp=1394041572420
        $arr_src = explode("/", $src);
        if (!$arr_src) {
           echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Link photo khong hop le!</td></tr>';
            $core->db->query("UPDATE zen_crawl_url_product SET  note='Link photo khong hop le!' WHERE id=$product->id");
            die;
        }
        $arr_src = array_slice($arr_src, 2);
        $arr_src[count($arr_src) - 2] = '1024';
        $src_photo = implode("/", $arr_src);
        $src_photo = 'http://' . $src_photo;

        $image_src = @file_get_contents($src_photo);
        if ($image_src) {
            $temp_file_name = $name . "_" . $product_data['product_code'] . "_zara";
            $temp_file_name = $core->convert_string_friendly($temp_file_name) . ".jpg";
            $folder_temp = $_SESSION['folder_temp'];
            $folder_thumb= "thumb";

            if (!is_dir(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp)) {
                mkdir(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp);
            }

            if (!is_dir(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp.DS.$folder_thumb)) {
                mkdir(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp.DS.$folder_thumb);
            }

            $temp_path = dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp . DS . $temp_file_name;

            if (!file_put_contents($temp_path, $image_src)) {
                echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Lay anh that bai file put content!!</td></tr>';
                $core->db->query("UPDATE zen_crawl_url_product SET  note='Lay anh that bai file put content!' WHERE id=$product->id");
                die;
            } else {
                $path_img_thumb = dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp . DS .$folder_thumb.DS. "200_" . $temp_file_name;
                copy($temp_path, $path_img_thumb);
                list ( $w, $h ) = getimagesize($temp_path);
                if ($w < 120 || $h < 120) {
                    echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>anh qua be, rong va cao > 120px!!</td></tr>';
                    $core->db->query("UPDATE zen_crawl_url_product SET  note='anh qua be, rong va cao > 120px!, link:'.$src_photo WHERE id=$product->id");
                    die;
                }
                chmod(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp, 0777);
                chmod($temp_path, 0777);
                //create image thumb
                $img_processor = new OSC_Image ();
                $img_processor->setImage($path_img_thumb)->setJpgQuality(100);
                $img_processor->resize(200)->save();
                $arr_product_extra_photo[] = $folder_temp .DS . $temp_file_name;
                $arr_product_thumb_extra_photo[] = $folder_temp . DS.$folder_thumb.DS . '200_' . $temp_file_name;
            }
        } else {
            echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Lay anh that bai:' . $src_photo . '!!</td></tr>';
            $core->db->query("UPDATE zen_crawl_url_product SET  note='Lay anh that bai:" . $src_photo . "' WHERE id=$product->id");
            die;
        }
    }

    if (count($arr_product_extra_photo) == 0 || count($arr_product_thumb_extra_photo) == 0) {
        echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>khong lay dc anh san pham: '.json_encode($arr_source_photo_product).'!</td></tr>';
        $check = json_encode($arr_source_photo_product);
        $core->db->query("UPDATE zen_crawl_url_product SET  note='khong lay dc anh san pham, check:'.$check WHERE id=$product->id");
        die;
    }

    $product_data['product_main_photo'] = $arr_product_extra_photo[0];
    $product_data['product_thumb_main_photo'] = $arr_product_thumb_extra_photo[0];
    $product_data['product_extra_photo'] = implode(",", $arr_product_extra_photo);
    $product_data['product_thumb_extra_photo'] = implode(",", $arr_product_thumb_extra_photo);

    //bo sung anh that cua san pham
//    $list_real_product_photo= array();
//    if ($product_html->find('div[class=left] div[class=bigImageContainer] div[class=imageZoom]')) {
//        $div_big_photo = $product_html->find('div[class=left] div[class=bigImageContainer] div[class=imageZoom]');
//        foreach($div_big_photo as $big_photo){
//            if ($big_photo->find('a', 0)) {
//                $big_photo_left = $big_photo->find('a', 0)->href;
//                if ($big_photo_left != '') {
//                    $list_real_product_photo[] = $big_photo_left;
//
//                } else {
//                    echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Khong tim thay tag SOURCE IMG real photo product column left</td></tr>';
//                    $core->db->query("UPDATE zen_crawl_url_product SET  note='Khong tim thay tag SOURCE IMG real photo product column left' WHERE id=$product->id");
//                    continue;
//                }
//            }
//        }
//    }
    //lấy ảnh phóng to của sản phẩm
    $arr_real_product_photo = array();
//    $count_color = 0;
//    if(is_array($data_src_big_photo)) {
//        foreach ($data_src_big_photo as $code => $data_big_photo) {
//            $temp_list_big_photo = array();
//            $data_big_photo = $data_big_photo['xmedias'];
//            foreach ($data_big_photo as $item_photo) {
//
//                if (isset($item_photo['url'])) {
//                    $src_real_photo = "http:" . $item_photo['url'];
//                    $image_src = @file_get_contents($src_real_photo);
//                    if ($image_src) {
//                        $temp_file_name = time() . rand(10000, 99999) . session_id() . '.jpg';
//                        $folder_temp = $_SESSION['folder_temp'];
//                        $folder_thumb = "thumb";
//
//                        if (!is_dir(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp)) {
//                            mkdir(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp);
//                        }
//
//                        if (!is_dir(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp . DS . $folder_thumb)) {
//                            mkdir(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp . DS . $folder_thumb);
//                        }
//
//                        $temp_path = dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp . DS . $temp_file_name;
//
//                        if (!file_put_contents($temp_path, $image_src)) {
//                            echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Lay anh real photo that bai file put content!!</td></tr>';
//                            $core->db->query("UPDATE zen_crawl_url_product SET  note='Lay anh real photo that bai file put content!' WHERE id=$product->id");
//                            continue;
//                        } else {
//                            $path_img_thumb = dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp . DS . $folder_thumb . DS . "200_" . $temp_file_name;
//                            copy($temp_path, $path_img_thumb);
//                            list ($w, $h) = getimagesize($temp_path);
//                            if ($w < 120 || $h < 120) {
//                                echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>anh qua be, rong va cao > 120px!!</td></tr>';
//                                $core->db->query("UPDATE zen_crawl_url_product SET  note='anh real photo qua be, rong va cao > 120px!, link:'.$src_photo WHERE id=$product->id");
//                                continue;
//                            }
//                            chmod(dirname(dirname(dirname(__FILE__))) . DS . "tmp_photo_crawl" . DS . $folder_temp, 0777);
//                            chmod($temp_path, 0777);
//                            $temp_list_big_photo[] = $folder_temp . DS . $temp_file_name;
//                        }
//                    } else {
//                        echo '<tr><td>' . $product->url_product . '</td><td>' . $product->product_name . '</td><td>////////</td><td>Lay anh real photo that bai:' . $src_real_photo . '!!</td></tr>';
//                        $core->db->query("UPDATE zen_crawl_url_product SET  note='Lay anh real photo that bai:" . $src_real_photo . "' WHERE id=$product->id");
//                        continue;
//                    }
//                } else {
//                    echo '<tr><td></td><td>' . $product->product_name . '</td><td>////////</td><td>Lay url photo that bai!!</td></tr>';
//                    $core->db->query("UPDATE zen_crawl_url_product SET  note='Lay url photo that bai' WHERE id=$product->id");
//                    continue;
//                }
//            }
//
//            $arr_real_product_photo[$arr_color_name[$count_color]] = $temp_list_big_photo;
//            $count_color++;
//        }
//    }

    $product_data['real_product_photo'] = json_encode($arr_real_product_photo);
    $product_data['thumb_real_product_photo'] = '';


    //check exist product code

    $check_product_code = $core->db->get_row("SELECT * FROM zen_crawl_product WHERE product_code ='".$product_data['product_code']."' LIMIT 1 ");
   if($check_product_code){
       $product_id= $check_product_code->id;
       $category_id= $product->menu_id;

   }else{
        //save product crawled
        $core->db->query("INSERT INTO zen_crawl_product (product_name,product_code,main_photo,
            thumb_main_photo,extra_photo,thumb_extra_photo,
            real_product_photo,thumb_real_product_photo,
            link_web,stamp_price,product_size,product_color)
            VALUES('" . $product_data['product_name'] . "','" . $product_data['product_code'] . "','" . $product_data['product_main_photo'] . "',
                    '" . $product_data['product_thumb_main_photo'] . "','" . $product_data['product_extra_photo'] . "','" . $product_data['product_thumb_extra_photo'] . "',
                    '" . $product_data['real_product_photo'] . "','" . $product_data['thumb_real_product_photo'] . "',
                        '" . $product->url_product . "'," . $product_data['product_stamp_price'] . ",'" . $product_data['product_size'] . "','" . $product_data['product_color_name'] . "')");
       $product_id= $core->db->insert_id;
       $category_id= $product->menu_id;

   }


    //check category and product id
    $check_product_category = $core->db->get_row("SELECT * FROM zen_crawl_category_product WHERE product_id =".$product_id." AND category_id=".$category_id."  LIMIT 1 ");
    if($check_product_category==null){
        $core->db->query("INSERT INTO zen_crawl_category_product (product_id,category_id) VALUES($product_id,$category_id)");
    }


    $core->db->query("UPDATE zen_crawl_url_product SET is_crawled=3 WHERE id=$product->id");
    sleep(2);
    //die("stop");

}

//$render_content.='</table>';
echo '</table>';
//echo $render_content;
die;
?>
